/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx.menu;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.util.Random;
import rpg.MListener;
import rpg.RPG;
import rpg.gfx.Bitmap;
import rpg.gfx.FontSets;
import rpg.util.ImageUtil;

/**
 *
 * @author rothens
 */
public class Menu extends Bitmap {
	int clicks = 0;
	int cticks = 0;
	Bitmap[] btn = {
		ImageUtil.loadBitmap("/res/menu_on.png"),
		ImageUtil.loadBitmap("/res/menu_off.png")
	};
	
	
	public class HotSpot {
		Bitmap textOn;
		Bitmap textOff;
		boolean active;
		int X,Y,right,bottom;
		
		public HotSpot(byte textId, String text, int Y){
			active = false;
			textOn = new Bitmap(btn[0].width, btn[0].height);
			textOff = new Bitmap(btn[0].width, btn[0].height);
			Bitmap textOnT = RPG.fs.getText(text, textId, 0xFF0000);
			Bitmap textOffT = RPG.fs.getText(text, textId, 0xFF0000);
			textOn.draw(btn[0], 0, 0)
				.draw(textOnT, textOn.width/2-textOnT.width/2, textOn.height/2-textOnT.height/2);
			
			textOff.draw(btn[1], 0, 0)
				.draw(textOffT, textOff.width/2-textOffT.width/2, textOff.height/2-textOffT.height/2);
			this.Y=Y;
			
			X=320-textOn.width/2;
			right=X+textOn.width;
			bottom=Y+textOn.height;
		}
		
		public boolean isMouseOver(int x, int y){
			return x>=X && x<right && y>=Y && y<bottom;

		}
		
		public Bitmap getPic(){
			if (active) return textOn;
			else return textOff;
		}
		
		public void activate(){
			
		}
		
	}
	
	private LinkedList<HotSpot> hotspots = new LinkedList<HotSpot>();

	Random r = new Random();
	
	private Bitmap cloudimg[] = {
		ImageUtil.loadBitmapScaled("/res/cloud.png", 0.3),
		ImageUtil.loadBitmapScaled("/res/cloud.png", 0.5),
		ImageUtil.loadBitmapScaled("/res/cloud.png", 0.8),
		ImageUtil.loadBitmap("/res/cloud.png"),
		ImageUtil.loadBitmap("/res/menugrass.png"),
		ImageUtil.loadBitmap("/res/menumnt.png")
	};
	
	
	private HotSpot active;
	private LinkedList<Cloud> clouds = new LinkedList<Cloud>();

	public Menu(int width, int height) {
		super(width, height);
		init();
	}

	public void init() {
		clearWith(0xCCCCFF);
		createCloud(0, 1, 2, 5);
		createCloud(1, 2, 4, 4);
		createCloud(2, 3, 5, 3);
		createCloud(3, 4, 7, 3);
		hotspots.add(new HotSpot((byte)2, "új játék", 140){

			@Override
			public void activate() {
				RPG.rpg.newGame();
			}
			
		});
		hotspots.add(new HotSpot((byte)2, "kilépés", 220){

			@Override
			public void activate() {
				System.exit(0);
			}
			
		});
		active = hotspots.peekFirst();
/*		clouds.add(new Cloud(cloudimg[5].width, 0, 480-cloudimg[5].height,1,5));
		clouds.add(new Cloud(cloudimg[5].width, 640, 480-cloudimg[5].height,1,5));
		clouds.add(new Cloud(cloudimg[4].width, 0, 480-cloudimg[4].height,2,4));
		clouds.add(new Cloud(cloudimg[4].width, 640, 480-cloudimg[4].height,2,4));*/
	}

	public void createCloud(int id, int minspeed, int maxspeed, int amt) {
		for (int i = 0; i < amt; i++) {
			clouds.add(new Cloud(cloudimg[id].width, r.nextInt(640), r.nextInt(350-cloudimg[id].height), minspeed + r.nextInt(maxspeed - minspeed), id));
		}
	}

	public void tick() {
		int mouseX = MListener.getInstance().mouseX;
		int mouseY = MListener.getInstance().mouseY;
		if(clicks>0){
			cticks++;
			if(cticks>10){
				cticks=0;
				clicks=0;
			}
		}
		for (Cloud c : clouds) {
			c.tick();
		}
		boolean act = false;
		for(HotSpot h : hotspots){
			if(h.isMouseOver(mouseX, mouseY)){
				act = true;
				h.active=true;
				active = h;
				mouseX=0;
				mouseY=0;
			} else {
				h.active=false;
			}
		}
		if(!act){
			active.active=true;
		}
		if(RPG.keylistener.isKeyDown(KeyEvent.VK_ESCAPE)){
			System.exit(0);
			return;
		}
		if(RPG.keylistener.isKeyDown(KeyEvent.VK_ENTER)){
			active.activate();
			return;
		}
		if(RPG.mouselistener.mouseClicked){
			int mX = RPG.mouselistener.X;
			int mY = RPG.mouselistener.Y;
			for(HotSpot h : hotspots){
				if(h.isMouseOver(mX, mY)){
					h.activate();
					return;
				}
			}
			clicks++;
			if(clicks == 2){
				//easter egg :D
				for(Cloud c: clouds){
					c.toggle();
				}
				cticks=0;
				clicks=0;
			}
		}
	}

	public void render() {
		clearWith(0xCCCCFF);
		for (Cloud c : clouds) {
			draw(cloudimg[c.type], c.X, c.Y);
		}

		for(HotSpot h : hotspots){
			draw(h.getPic(), h.X, h.Y);
		}
	}
}
